package org.xg.study.pd.impl;

import com.alibaba.fastjson.JSONObject;
import org.xg.study.pd.HttpClient;
import org.xg.study.pd.NetMall;
import sun.misc.BASE64Encoder;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author CMG
 * @version 1.0
 * @description
 * @cLassName DangDangNetMall
 * @date 2021/9/18 9:40
 **/
public class DangDangNetMall extends NetMall {
    public DangDangNetMall(String uId, String uPwd) {
        super(uId, uPwd);
    }

    @Override
    protected Boolean login(String uId, String uPwd) {
        logger.info("模拟当当用户登陆 uId:{}, uPwd:{}",uId,uPwd);
        return true;
    }

    @Override
    protected Map<String, String> reptile(String skuUrl) {
        String str = HttpClient.doGet(skuUrl);
        Pattern p9 = Pattern.compile("(?<=title\\\\>).*(?=</title)");
        Matcher m9 = p9.matcher(str);
        Map<String,String> map = new ConcurrentHashMap<>();
        if(m9.find()){
            map.put("name",m9.group());
        }
        map.put("price","4598.00");
        logger.info("模拟当当商品爬虫解析：{} | {} 元 {}",map.get("name"),map.get("price"),skuUrl);
        return map;
    }

    @Override
    protected String createBase64(Map<String, String> goodsInfo) {
        BASE64Encoder encoder = new BASE64Encoder();
        logger.info("模拟生成当当商品base64海报");
        return encoder.encode(JSONObject.toJSONString(goodsInfo).getBytes());
    }
}
